package com.guo.hui.shiro;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.cxf.transport.http.policy.HTTPServerAssertionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.guo.hui.controller.BaseController;
import com.guo.hui.dao.shiro.TpRoleMapper;
import com.guo.hui.enumtype.DelFlag;
import com.guo.hui.model.shiro.TpRole;
import com.guo.hui.page.PageParam;
import com.guo.hui.page.PageResult;
import com.guo.hui.service.RoleService;
import com.guo.hui.util.UtilJson;

@RequestMapping("role")
@Controller
public class RoleController extends BaseController {

	@Autowired
	private RoleService roleService;
	@Autowired
	private TpRoleMapper roleDao;

	@RequestMapping("/role")
	public String index() {
		return "blog/role/role";
	}

	@ResponseBody
	@RequestMapping("/roledata")
	public String roledata(Model mm) {
		List<TpRole> list = roleService.selectByMap(new HashMap<String, Object>());
		return UtilJson.toJson(list);
	}

	@ResponseBody
	@RequestMapping("/roledatalist")
	public String roledatalist(HttpServletRequest request, String map) {
		// System.out.println(request.getParameter("map"));
		// Gson gson = new Gson();
		// Map<String, Object> parms = new HashMap<String, Object>();
		// parms = gson.fromJson(map, parms.getClass());
		// System.out.println(parms);
		Map<String, Object> parms = new HashMap<String, Object>();
		parms = UtilJson.fromJsonType(map, parms.getClass());
		PageParam pageParm = new PageParam();
		pageParm.setPage(Integer.valueOf(request.getParameter("page")));// 页数
		pageParm.setPageSize(Integer.valueOf(request.getParameter("rows")));// 条数
		pageParm.setSearchContent(parms);
		PageInfo<TpRole> pagelist = roleService.selectByMapPage(pageParm);
		PageResult result = new PageResult();
		result.setTotal((int) pagelist.getTotal());
		result.setRows(pagelist.getList());
		return UtilJson.toJson(result);
	}

	@RequestMapping("/roleadd")
	public String roleadd() {
		return "blog/role/roleadd";
	}

	@ResponseBody
	@RequestMapping("/rolesave")
	public String usersave(@RequestParam String role) {
		TpRole tp = (TpRole) UtilJson.fromJson(role, TpRole.class);
		roleService.saveRole(tp);
		return "ok";
	}

	@ResponseBody
	@RequestMapping("/roledelete")
	public String userdelete(@RequestParam int id) {
		TpRole tp = new TpRole();
		tp.setId(id);
		tp.setDelflag(DelFlag.LOSE.toString());
		roleService.updateRole(tp);
		return "ok";
	}

	@RequestMapping("/update")
	public String update(Model mm, @RequestParam int id) {
		TpRole tp = roleDao.selectById(id);
		mm.addAttribute("tp", tp);
		return "blog/role/update";
	}

	@ResponseBody
	@RequestMapping("/roleupdate")
	public String roleupdate(@RequestParam String tp) {
		TpRole role = (TpRole) UtilJson.fromJson(tp, TpRole.class);
		roleService.updateRole(role);
		return "ok";
	}

	@ResponseBody
	@RequestMapping("/userrole")
	public String userrole(@RequestParam Integer userid, String ids) {
		Integer[] roleids = (Integer[]) UtilJson.fromJson(ids, Integer[].class);
		roleService.saveUserRole(userid, roleids);
		return "ok";
	}

}
